/*
 * PerformanceController
 *
 * Copyright (c) 2018 Manulife International Ltd.
 *
 * Description:
 *
 * Maintenance History
 *
 * YYMMDD  Who              Reason
 * ======= ================ ====================================================
 * 180209  Eric Zhang        Application development
 *
 
package com.manulife.hk.performance.controllers;

import com.manulife.hk.performance.domains.SubmitSaleEntity;
import com.manulife.hk.performance.exception.EntityNotFoundException;
import com.manulife.hk.performance.query.PageQuery;
import com.manulife.hk.performance.services.PerformanceService;
import com.manulife.hk.performance.validation.Add;
import com.manulife.hk.performance.validation.Update;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class PerformanceController {

    @Autowired
    private PerformanceService performanceService;

    *//**
     * Get all performance record
     * @return a list of performance record
     *//*
    @GetMapping("/v1/performance/")
    public ResponseEntity<List<SubmitSaleEntity>> listAll() {
        List<SubmitSaleEntity> plist = performanceService.findAll();
        if(plist.isEmpty()){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<>(plist, HttpStatus.OK);
    }


    *//**
     * Get performance record by id
     * @param id performance id
     * @return one performance record matched
     * @throws EntityNotFoundException
     *//*
    @GetMapping("/v1/performance/{id}")
    public ResponseEntity<?> getPerformance(@PathVariable("id") Long id) throws EntityNotFoundException {
        SubmitSaleEntity performance = performanceService.findById(id);
        if(null == performance){
            return new ResponseEntity<>(new SubmitSaleEntity(), HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(performance, HttpStatus.OK);
    }

    *//**
     * Create one performance
     * @param performance one performance record
     * @return successfully created performance
     *//*
    @PostMapping("/v1/performance/")
    public ResponseEntity<?> createPerformance(@Validated(Add.class) @RequestBody SubmitSaleEntity performance) {
        if(performanceService.isExisted(performance)){
            return new ResponseEntity<>("Unable to create. A  SubmitSales with id " +
                    performance.getId() + " already exist.",HttpStatus.CONFLICT);
        }
        SubmitSaleEntity created = performanceService.add(performance);
        return new ResponseEntity<>(created, HttpStatus.CREATED);
    }

    *//**
     * Update performance record by id
     * @param id performance id
     * @param performance updated performance record
     * @return updated performance
     * @throws EntityNotFoundException
     *//*
    @PutMapping("/v1/performance/{id}")
    public ResponseEntity<?> update(@PathVariable("id") Long id,@Validated(Update.class) @RequestBody SubmitSaleEntity performance)
            throws EntityNotFoundException {
        log.info("Updating User with id {}", id);

        SubmitSaleEntity existed = performanceService.findById(id);

        if (null == existed) {
            log.error("Unable to update. SubmitSales with id {} not found.", id);
            return new ResponseEntity<>("Unable to upate. SubmitSales with id " + id + " not found.",
                    HttpStatus.NOT_FOUND);
        }
        performanceService.update(performance);
        return new ResponseEntity<>(performance, HttpStatus.OK);
    }

    *//**
     * Remove a performance record by id
     * @param id performance id
     * @return
     * @throws EntityNotFoundException
     *//*
    @DeleteMapping("/v1/performance/{id}")
    public ResponseEntity<?> remove(@PathVariable("id") Long id) throws EntityNotFoundException {
        SubmitSaleEntity existed= performanceService.findById(id);
        if (null == existed) {
            log.error("Unable to delete. SubmitSales with id {} not found.", id);
            return new ResponseEntity<>("Unable to delete. SubmitSales with id " + id + " not found.",
                    HttpStatus.NOT_FOUND);
        }
        log.info("Remove the performance record: {}",existed.toString());
        performanceService.remove(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    *//**
     * Remove all performance records
     * @return
     *//*
    @DeleteMapping(value = "/v1/performance/")
    public ResponseEntity<?> deleteAllPerformance() {
        log.info("Deleting All SubmitSales");
        performanceService.removeAll();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    *//**
     * Find by criteria
     * @param pageQuery page query information
     * @param performance search bean
     * @return a list of SubmitSales that match the criteria
     *//*
    @GetMapping(value = "/v1/performance/search/")
    public ResponseEntity<Page<SubmitSaleEntity>> findByCriteria(PageQuery pageQuery, SubmitSaleEntity performance) {
        log.info("PageQuery: {}",pageQuery.toString(),"Criteria: {}", performance.toString());
        Page<SubmitSaleEntity> page = performanceService.findByCriteria(getPageabeObject(pageQuery),performance);
        if(!page.hasContent()){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
       return new ResponseEntity<>(page, HttpStatus.OK);
    }

    private Pageable getPageabeObject(PageQuery pageQuery){
        return new PageRequest(pageQuery.getPageNum(),
                pageQuery.getPageSize(),
                new Sort(Sort.Direction.fromString(pageQuery.getDirection()),
                        pageQuery.getColumn()));
    }
}
*/